package hoyo.zqy.order.service.impl;

import hoyo.zqy.basic.util.CodeGenerateUtils;
import hoyo.zqy.basic.util.DistanceUtils;
import hoyo.zqy.basic.util.Point;
import hoyo.zqy.order.domain.OrderProduct;
import hoyo.zqy.order.mapper.OrderProductMapper;
import hoyo.zqy.order.service.IOrderProductService;
import hoyo.zqy.basic.service.impl.BaseServiceImpl;
import hoyo.zqy.org.domain.Shop;
import hoyo.zqy.org.mapper.EmployeeMapper;
import hoyo.zqy.org.mapper.ShopMapper;
import hoyo.zqy.product.domain.Product;
import hoyo.zqy.product.mapper.ProductMapper;
import hoyo.zqy.user.domain.Logininfo;
import hoyo.zqy.user.domain.User;
import hoyo.zqy.user.domain.UserAddress;
import hoyo.zqy.user.mapper.UserAddressMapper;
import hoyo.zqy.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class OrderProductServiceImpl extends BaseServiceImpl<OrderProduct> implements IOrderProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderProductMapper orderProductMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private ShopMapper shopMapper;


    @Override
    public void submit(Map<String, Object> params, Logininfo logininfo) {
        //1获取前端传来的4个参数
        Long serviceMethod = Long.valueOf(params.get("service_method").toString());//送货方式
        Long addressId = Long.valueOf(params.get("address_id").toString());//地址
        Long payMethod = Long.valueOf(params.get("pay_method").toString());// 0 支付宝 1 微信  2 银联
        Long productId = Long.valueOf(params.get("product_id").toString());//productid
        //获取服务信息
        Product product = productMapper.loadById(productId);
        //绑定服务用户
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        //生成服务订单
        //获取用户信息
        UserAddress address = userAddressMapper.loadById(addressId);
        Point point = DistanceUtils.getPoint(address.getFullAddress());
        //获取所有的shops
        List<Shop> shops = shopMapper.loadAll();
        //填入订单信息
        OrderProduct order = initProductOrder(product, user,point,shops);
        orderProductMapper.save(order);
    }

    /**
     * 完善订单信息
     * @param product
     * @param user
     * @param point
     * @param shops
     * @return
     */
    private OrderProduct initProductOrder(Product product, User user, Point point, List<Shop> shops) {
        OrderProduct order = new OrderProduct();
        order.setDigest("[摘要]对"+product.getName()+"服务订单！");
        order.setState(0);//O待支付 1待收货
        BigDecimal saleprice = product.getSaleprice();
        order.setPrice(product.getSaleprice());//售价
        String orderSn = CodeGenerateUtils.generateOrderSn(user.getId());
        order.setOrderSn(orderSn);//订单唯一标识
        order.setShopId(product.getId());
        order.setUserId(user.getId());
        Shop nearestShop = DistanceUtils.getNearestShop(point, shops);
        order.setShopId(nearestShop.getId());
        //最后支付时间
        order.setLastPayTime(new Date(System.currentTimeMillis() + 15*60*1000));
        return order;
    }
}
